<template>
  <div class="achievement" v-cloak>
    <div class="bg_color"></div>
    <div style="height: 4px;position: absolute;z-index: 2222; width: 100%; top: 46px;background: #f0f0f0"></div>
    <div style="height: 50px;"></div>
    <div class="about-us" v-show="show">
      <div class="content" v-if="isshow">
        <ul class="list">
          <li v-for="(item,index) in list">
            <p>{{item.ua_name}}<span>{{item.ua_mobile}}</span></p>
            <p>{{item.ua_address_detail}}</p>
            <div class="handles">
              <div class="checkbox">
                <input @click="setDefault(item.ua_default,item.ua_id)" type="radio" :id="'radio'+index" name="choice" :checked="{ 'checked' : item.ua_default == '是'}">
                <label>默认地址</label>
              </div>
              <a class="revise" @click="reviseAddress(item)"><i></i>修改</a>
              <a class="del" @click="delmange(item.ua_id,index)"><i></i>删除</a>
            </div>
          </li>
        </ul>
        <div class="bottom">
          <div class="bottomBtn" @click="addaddress">新增地址</div>
        </div>
      </div>
      <div class="noshow" v-else>
         暂无地址
      </div>
      <div class="withdraw"  v-if="del">
        <div class="withdrawContent" >
          <p class="title">确定要删除吗？</p>
          <div class="btnGroup">
            <span class="cancel" @click="canceldel">取消</span>
            <span class="submit" @click="delAddressBook">确定</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
  export default {
    name: 'ManageAddress',
    data () {
      return {
        show: false,
        list: [],
        isshow: true,
        del: false,
        uaid: '',
        currenindex: ''
      }
    },
    methods: {
      setDefault: function (status, id) {
        var thisObj = this
        if (status === '是') {
          console.log(id)
          thisObj.$chaos.ajax({
            data: {
              id: id
            },
            slient: true,
            userinfo: true,
            url: 'User/setDefaultAddress',
            callback: function (type, ret) {
              if (ret.status === 'SUCCESS') {
                thisObj.$vux.toast.show({
                  text: ret.result.msg,
                  type: 'text',
                  width: '180px',
                  position: 'bottom'
                })
              } else {
                thisObj.$vux.toast.show({
                  text: ret.result.msg,
                  type: 'text',
                  width: '180px',
                  position: 'bottom'
                })
              }
            }
          })
        }
      },
      reviseAddress: function (item) {
        this.setmode('manger', 'one')
        window.location.href = '#/ReviseAddress?ua_name=' + encodeURI(item.ua_name) + '&ua_mobile=' + encodeURI(item.ua_mobile) + '&ua_address=' + encodeURI(item.ua_address) + '&ua_address_detail=' + encodeURI(item.ua_address_detail) + '&ua_default=' + encodeURI(item.ua_default) + '&ua_id=' + encodeURI(item.ua_id)
      },
      delAddressBook: function () {
        var thisObj = this
        thisObj.$chaos.ajax({
          data: {
            id: thisObj.uaid
          },
          slient: true,
          userinfo: true,
          url: 'User/delUserAddress',
          callback: function (type, ret) {
            if (ret.status === 'SUCCESS') {
              thisObj.$vux.toast.show({
                text: ret.result.msg,
                type: 'text',
                width: '180px',
                position: 'bottom'
              })
              thisObj.del = false
              thisObj.list.splice(thisObj.currenindex, 1)
              if (thisObj.list.length === 0) {
                thisObj.setmode('deletemanger', 'delete')
              }
            } else {
              thisObj.$vux.toast.show({
                text: ret.result.msg,
                type: 'text',
                width: '180px',
                position: 'bottom'
              })
            }
          }
        })
      },
      canceldel: function () {
        this.del = false
      },
      delmange: function (uaid, index) {
        this.uaid = uaid
        this.del = true
        this.currenindex = index
      },
      addaddress: function () {
        window.location.href = '#/Address?member=' + 'no'
      },
      setmode: function (name, content) {
        let mode = this.$chaos.config('mode')
        if (mode === 'website') {
          sessionStorage[name] = JSON.stringify(content)
        } else {
          localStorage[name] = JSON.stringify(content)
        }
      },
      verify: function () {
        var thisObj = this
        this.setmode('need-refresh', true)
        this.$chaos.setTitle('地址管理')
        this.$store.commit('headerShow', true)
        this.$store.commit('title', '地址管理')
        this.$store.commit('footerShow', false)
        thisObj.$chaos.ajax({
          data: {
            type: '服务地址'
          },
          slient: true,
          userinfo: true,
          url: 'User/getUserAddress',
          callback: function (type, ret) {
            if (ret.status === 'SUCCESS') {
              if (ret.result.address_list.length === 0) {
                thisObj.isshow = false
                return false
              }
              thisObj.list = ret.result.address_list
              thisObj.show = true
            } else {
              thisObj.$vux.toast.show({
                text: ret.result.msg,
                type: 'text',
                width: '180px',
                position: 'bottom'
              })
            }
          }
        })
      }
    },
    created: function () {
      this.verify()
    },
    mounted: function () {
      window.scrollTo(0, 2000)
    },
    watch: {
      '$route': 'verify'
    }
  }
</script>
<style scoped>
  [v-cloak] {
    display: block;
  }
  .content {
    /*padding-top: 48px;*/
    padding-bottom: 50px;
  }
  .content .list li {
    padding-top: 10px;
    background: #fff;
    margin-bottom: 2px;
  }
  .content .list li > p {
    height: 25px;
    line-height: 25px;
    padding: 0 15px;
  }
  .content .list li > p {
    height: 25px;
    line-height: 25px;
    padding: 0 15px;
  }
  .content .handles {
    height: 40px;
    background: #fff;
    position: relative;
    margin-top: 10px;
    border-top: 1px solid #eee;
  }
  .content .handles .checkbox {
    position: absolute;
    height: 40px;
    line-height: 40px;
    left: 15px;
  }
  .content .handles .checkbox > input[type="radio"] {
    position: absolute;
    left: 0;
    top: 11px;
    outline: none;
    -webkit-appearance: none;
    border: 1px solid #ccc;
    border-radius: 3px;
    height: 16px;
    width: 16px;
    margin-right: 5px;
  }
  .content .handles .checkbox label {
    margin-left: 25px;
  }
  .content .handles .revise {
    right: calc(10% + 50px);
  }
  .content .handles > a {
    width: 50px;
    height: 40px;
    line-height: 40px;
    position: absolute;
    text-align: right;
    font-size: 14px;
  }
  .content .handles .checkbox > input[type="radio"]:checked{
    background: url(../assets/image/fuxq_btn_02.png) no-repeat;
    background-size: 100% 100%;
    width:16px;
    height:16px;
    border:0;
  }
  .content .handles i {
    width: 16px;
    height: 16px;
    position: absolute;
    top: 12px;
    left: 0;
  }
  .content .handles .revise i {
    background: url(../assets/image/dzb_01.png);
    background-size: 100% 100%;
  }
  .content .handles .del {
    right: 15px;
  }
  .content .handles .del i {
    background: url(../assets/image/dzb_02.png);
    background-size: 100% 100%;
  }
  .content .bottom {
    width: 100%;
    height: 48px;
    background: #fff;
    position: fixed;
    bottom: 0;
  }
  .content .bottom .bottomBtn {
    background: rgb(255,160,40);
    color: #f5f5f5;
    width: 92%;
    height: 37px;
    line-height: 37px;
    text-align: center;
    margin: 6px auto;
    border-radius: 4px;
    font-size: 15px;
  }
  .withdraw{
    position: fixed;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    background: rgba(50,50,50,0.5);
    z-index: 100000
  }
  .withdraw .withdrawContent{
    width: 60%;
    background: #fff;
    left: 20%;
    top: calc(50% - 80px);
    border-radius: 10px;
    position: fixed;
    padding-top: 10px
  }
  .withdraw .withdrawContent .title{
    height: 50px;
    line-height: 50px;
    text-align: center;
  }
  .withdraw .withdrawContent .title i{
    display: inline-block;
    width: 16px;
    height: 16px;
    /*background: url("../assets/image/info.png");*/
    background-size: 100% 100%;
    margin-right: 5px;
  }
  .withdraw .withdrawContent .text{
    height: 40px;
    line-height: 40px;
    font-size: 14px;
    text-align: center;
  }
  .withdraw .withdrawContent .btnGroup{
    width: 80%;
    height: 70px;
    position: relative;
    margin: 0 auto;
  }
  .withdraw .withdrawContent .btnGroup > span{
    width: 40%;
    height: 28px;
    line-height: 26px;
    display: block;
    position: absolute;
    text-align: center;
    border: 1px solid rgb(245,38,116);
    border-radius: 8px;
    box-sizing: border-box;
    top: 15px;
  }
  .withdraw .withdrawContent .btnGroup .cancel{
    left: 5%;
    background: #fff;
    color: rgb(255,160,40);
    border: 1px solid rgb(255,160,40)
  }
  .withdraw .withdrawContent .btnGroup .submit{
    right: 5%;
    background:rgb(255,160,40);
    color: #fff;
    border: 1px solid rgb(255,160,40)
  }
</style>
